<%@ page language="java" contentType="text/html;charset=utf-8"
	pageEncoding="utf-8"%>
<%@ page import="com.sms.project.*"%>
<%@ page import="net.sf.json.*"%>
<%@ page import="java.util.List,java.util.ArrayList"%>

<%--
	project/teacher/modify-time.jsp
 --%>

<%
	//如果修改页面信息，则更新数据库的数据
	String msg = "";
	boolean isSuccess = true;
	String row = request.getParameter("row");
	String action = request.getParameter("action");
	
	if(action != null && action.trim().equals("modify")) {
		int proID = Integer.parseInt(request.getParameter("proID"));
		int groupID = Integer.parseInt(request.getParameter("groupID"));
		
		int grade = Integer.parseInt(request.getParameter("grade"));
		int newGrade = Integer.parseInt(request.getParameter("newGrade"));
		
		String leaderID = request.getParameter("leaderID");
		String newLeader = request.getParameter("newLeader");
		if(!newLeader.matches("^[0-9]*$")) {
			newLeader = leaderID;
		}
		
		String memberID = request.getParameter("memberID");
		String[] member = memberID.split(",");
		String newMember[] = request.getParameterValues("newMember");
		
		String endTime = request.getParameter("endTime");
		String newTime = request.getParameter("newEndtime");
		
		if(isSuccess && newTime != null) {
			if(!newTime.trim().equals("")) {
				if(!Task.modifyTime(proID, endTime, newTime)) {
					isSuccess = false;
				}
			} else {
				isSuccess = false;
			}
		}
		
		if(isSuccess) {
			if(newGrade == 3 || newGrade == 2) {
				if(!Task.modifyGrade(proID, grade, newGrade)) {
					isSuccess = false;
				}
			} else {
				isSuccess = false;
			}
		}
		
		if(isSuccess && newLeader != null) {
			if(!newLeader.trim().equals("") ) {
				if(!newLeader.trim().equals(leaderID)) {
					if(!Task.modifyLeader(groupID, leaderID, newLeader, member)) {
						isSuccess = false;
					}
				}
			} else {
				isSuccess = false;
			}
		}
		
		if(isSuccess && newMember != null) {
			if(newMember.length != 0) {
				if(!Task.modifyMemeber(groupID, leaderID, member, newMember, newLeader)) {
					isSuccess = false;
				}
			} else {
				isSuccess = false;
			}
		} 
		
	} else {
		isSuccess = false;
	}
	
	if(isSuccess) {
		msg = "ok";
	} else {
		msg = "error";
	}
	
	msg = msg.replace("/rn/g", "");//去掉产生的换行
		
	JSONArray json = new JSONArray();
	JSONObject j = new JSONObject();
	j.put("text", msg);
	j.put("row", row);
	json.add(j);
	
	out.clear();
	out.println(json);
%>